package xhq.com.test.extspring.listener;

import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * 需要配置到 resources/META-INF/spring.factories 文件中，指定到配置项 org.springframework.boot.SpringApplicationRunListener 中去
 * @author hq_xu
 * @since 2021/1/26 9:44
 */
public class MySpringApplicationRunListener implements SpringApplicationRunListener {
    private SpringApplication application = null;
    private String[] args = null;

    /**
     * 一定要有这个签名的构建函数
     */
    public MySpringApplicationRunListener(SpringApplication sa, String[] args) {
        this.application = sa;
        this.args = args;
    }

    @Override
    public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
        System.out.println("from MySpringApplicationRunListener");
        var source = environment.getPropertySources();
        var iterator = source.iterator();
        while (iterator.hasNext()) {
            var currentSource = iterator.next();
            System.out.println(currentSource.getName());
        }
    }
}
